package com.server.file.utils;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.server.model.all.enums.BucketNameEnum;
import com.server.model.all.enums.BusinessNameEnum;
import com.server.common.code.error.BusinessException;
import com.server.common.security.code.SecurityUtils;
import com.server.commons.file.model.input.UploadFileInput;
import com.server.commons.file.utils.UploadFileUtils;
import org.springframework.web.multipart.MultipartFile;

public class UploadFileInputBuilder {

    private final UploadFileInput uploadFileInput;

    public UploadFileInputBuilder(){
        this.uploadFileInput = new UploadFileInput();
        this.uploadFileInput.setFileCode(UploadFileUtils.createFileCode());
        this.uploadFileInput.setUserCode(SecurityUtils.getLoginVo().getUserCode());
    }
    
    public UploadFileInput build(){
        return uploadFileInput;
    }

    public static UploadFileInputBuilder builder(){
        return new UploadFileInputBuilder();
    }

    public UploadFileInputBuilder multipartFile(MultipartFile file){
        try {
            this.uploadFileInput.setSuffix(FileUtil.getSuffix(file.getOriginalFilename()));
            this.uploadFileInput.setBytes(IoUtil.readBytes(file.getInputStream()));
            this.uploadFileInput.setSuffix(FileUtil.getSuffix(file.getOriginalFilename()));
        }catch (Exception e){
            throw new BusinessException("上传文件有误!");
        }
        return this;
    }

    public UploadFileInputBuilder businessName(BusinessNameEnum businessName){
        this.uploadFileInput.setBusinessNameEnum(businessName);
        return this;
    }

    public UploadFileInputBuilder bucketName(BucketNameEnum bucketName){
        this.uploadFileInput.setBucketNameEnum(bucketName);
        return this;
    }


}
